home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_008 / src / hack.window.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  408 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/io.h>
  5. #include <intuition/intuition.h>
  6. #include <stdio.h>
  7. #include "hack.h"   /* for ROWNO and COLNO */
  8. #include "def.arrows.h"
  9. #include "def.menus.h"
  10.  
  11. #define XSIZE 8
  12. #define YSIZE 8
  13. #define BASEX 4
  14. #define BASEY 4   /* should be -4 */
  15.  
  16. #define ICON_REV       0
  17. #define GRAPHICS_REV   29
  18. #define INTUITION_REV  29
  19.  
  20. struct IntuitionBase *IntuitionBase;
  21. struct GfxBase *GfxBase;
  22. char *IconBase;
  23. int mousex, mousey;
  24.  
  25. #define BUFFERED 128
  26. char out_buffer[BUFFERED];
  27. int bufcnt;
  28. char *out_bufp;
  29.  
  30. struct Image URImage = { 0,0,22,10,2,(short *)&UPRIGHTdata,   0x3, 0, NULL };
  31. struct Image DRImage = { 0,0,22,10,2,(short *)&DOWNRIGHTdata, 0x3, 0, NULL };
  32. struct Image RImage  = { 0,0,22,10,2,(short *)&RIGHTdata,     0x3, 0, NULL };
  33. struct Image DImage  = { 0,0,22,10,2,(short *)&DOWNdata,      0x3, 0, NULL };
  34. struct Image UImage  = { 0,0,22,10,2,(short *)&UPdata,        0x3, 0, NULL };
  35. struct Image LImage  = { 0,0,22,10,2,(short *)&LEFTdata,      0x3, 0, NULL };
  36. struct Image DLImage = { 0,0,22,10,2,(short *)&DOWNLEFTdata,  0x3, 0, NULL };
  37. struct Image ULImage = { 0,0,22,10,2,(short *)&UPLEFTdata,    0x3, 0, NULL };
  38.  
  39. struct Gadget URGadget = { NULL,      436, 0, 22,10,
  40.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  41.             (APTR)&URImage, NULL, NULL, 0, NULL, 'u', NULL };
  42. struct Gadget DRGadget = { &URGadget, 568, 0, 22,10,
  43.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  44.             (APTR)&DRImage, NULL, NULL, 0, NULL, 'n', NULL };
  45. struct Gadget RGadget  = { &DRGadget, 490, 0, 22,10,
  46.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  47.             (APTR)&RImage, NULL, NULL, 0, NULL, 'l', NULL };
  48. struct Gadget DGadget  = { &RGadget,  544, 0, 22,10,
  49.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  50.             (APTR)&DImage, NULL, NULL, 0, NULL, 'j', NULL };
  51. struct Gadget UGadget  = { &DGadget,  412, 0, 22,10,
  52.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  53.             (APTR)&UImage, NULL, NULL, 0, NULL, 'k', NULL };
  54. struct Gadget LGadget  = { &UGadget,  466, 0, 22,10,
  55.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  56.             (APTR)&LImage, NULL, NULL, 0, NULL, 'h', NULL };
  57. struct Gadget DLGadget = { &LGadget,  520, 0, 22,10,
  58.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  59.             (APTR)&DLImage, NULL, NULL, 0, NULL, 'b', NULL };
  60. struct Gadget ULGadget = { &DLGadget, 388, 0, 22,10,
  61.             GADGHCOMP | GADGIMAGE, GADGIMMEDIATE, BOOLGADGET,
  62.             (APTR)&ULImage, NULL, NULL, 0, NULL, 'y', NULL };
  63.  
  64. struct Window *HackWindow;
  65. struct NewWindow NewHackWindow = {
  66.   0,1,640,199, -1,-1,   /* left, top, width, height, detailpen, blockpen */
  67.   MOUSEBUTTONS | CLOSEWINDOW | RAWKEY | MENUPICK | GADGETDOWN,
  68.   WINDOWDEPTH | WINDOWCLOSE | ACTIVATE,
  69.   &ULGadget, NULL, "HACK V1.0.1 - Ported by John A. Toebes, VIII",
  70.   NULL, NULL, 640,200,640,200, WBENCHSCREEN };
  71.  
  72. struct IOStdReq consoleIO;
  73.  
  74. #define HO "\x9BH"
  75. #define CL "\x0C"
  76. #define CE "\x9BK"
  77. #define UP "\x0B"
  78. #define CM "\x9B%d;%dH"
  79. #define ND "\x09"
  80. #define XD "\x9BB"
  81. #define BC "\x08"
  82. #define SO "\x9B4m"
  83. #define SE "\x9B0m"
  84. #define BELL 7
  85. int myx, myy;
  86.  
  87. startup()
  88. {
  89. }
  90.  
  91. /* Cursor movements */
  92. extern xchar curx, cury;
  93.  
  94. curs(x,y)
  95. register int x,y;   /* not xchar: perhaps xchar is unsigned and
  96.             curx-x would be unsigned as well */
  97. {
  98.    if (y != cury || x != curx)
  99.     myprintf(CM, y, x);
  100.    curx = x;
  101.    cury = y;
  102. }
  103.  
  104. cl_end() {
  105.     myprintf(CE);
  106. }
  107.  
  108. clear_screen() {
  109.     myprintf(CL);
  110.     curx = cury = 1;
  111. }
  112.  
  113. home()
  114. {
  115.     myprintf(HO);
  116.     curx = cury = 1;
  117. }
  118.  
  119. standoutbeg()
  120. {
  121.     myprintf(SO);
  122. }
  123.  
  124. standoutend()
  125. {
  126.     myprintf(SE);
  127. }
  128.  
  129. backsp()
  130. {
  131.     myprintf(BC);
  132.     curx--;
  133. }
  134.  
  135. bell()
  136. {
  137.     myputchar(BELL);
  138. }
  139.  
  140. delay_output()
  141. {
  142.    /* delay 40 ms, 50 ticks/sec    */
  143.    Delay (2);
  144. }
  145.  
  146. initterm()
  147.    {
  148.    if ( (IntuitionBase = (struct IntuitionBase *)
  149.     OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  150.     _exit(2);
  151.  
  152.    if ( (GfxBase = (struct GfxBase *)
  153.     OpenLibrary("graphics.library",GRAPHICS_REV)) == NULL)
  154.     _exit(3);
  155.  
  156.    if ( (IconBase = (char *)
  157.     OpenLibrary("icon.library",ICON_REV)) == NULL)
  158.     _exit(4);
  159.  
  160.    if ( (HackWindow = (struct Window *)
  161.     OpenWindow(&NewHackWindow)) == NULL)
  162.     _exit(5);
  163.  
  164.    SetMenuStrip(HackWindow,&HackMenu);
  165.  
  166.    consoleIO.io_Data = (APTR) HackWindow;
  167.    consoleIO.io_Length = sizeof(*HackWindow);
  168.    if (OpenDevice("console.device",0, &consoleIO, 0) != 0)
  169.     hackexit(6);
  170.  
  171.    bufcnt = 0;
  172.    out_bufp = out_buffer;
  173.    }
  174.  
  175. hackexit(code)
  176. int code;
  177.    {
  178.    CloseDevice(&consoleIO);
  179.    ClearMenuStrip(HackWindow);
  180.    CloseWindow(HackWindow);
  181.    CloseLibrary(IconBase);
  182.    CloseLibrary(GfxBase);
  183.    CloseLibrary(IntuitionBase);
  184.    _exit(code);
  185.    }
  186.  
  187. myfflush()
  188.    {
  189.    if (bufcnt)
  190.     {
  191.     consoleIO.io_Command = CMD_WRITE;
  192.     consoleIO.io_Data = (APTR)out_buffer;
  193.     consoleIO.io_Length = bufcnt;
  194.     DoIO(&consoleIO);
  195.     }
  196.    bufcnt = 0;
  197.    }
  198.  
  199. myputchar(c)
  200. char c;
  201.    {
  202.     if (bufcnt == BUFFERED)
  203.         myfflush();
  204.  
  205.     out_buffer[bufcnt++] = c;
  206.    }
  207.  
  208. myputs(str)
  209. char *str;
  210.    {
  211.     int len, tocopy;
  212.  
  213.     len = strlen(str);
  214.  
  215.     if (len >= BUFFERED)
  216.         {
  217.         myfflush();
  218.         consoleIO.io_Command = CMD_WRITE;
  219.         consoleIO.io_Data = (APTR)str;
  220.         consoleIO.io_Length = len;
  221.         DoIO(&consoleIO);
  222.         }
  223.     else
  224.         {
  225.         if (bufcnt+len >= BUFFERED) /* is there room */
  226.             {
  227.             tocopy = BUFFERED - bufcnt;
  228.             movmem(str, &out_buffer[bufcnt], tocopy);
  229.             bufcnt += tocopy;
  230.             len -= tocopy;
  231.             str += tocopy;
  232.             myfflush();
  233.             }
  234.         if (len)
  235.             {
  236.             /* just move it in */
  237.             movmem(str, &out_buffer[bufcnt], len);
  238.             bufcnt += len;
  239.             }
  240.         }
  241.     myputchar('\n');
  242.    }
  243.  
  244. /*VARARGS1*/
  245. myprintf(str,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  246. char *str,*a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9;
  247.    {
  248.     char buf[BUFFERED], *bptr;
  249.     int len, tocopy;
  250.  
  251.     bptr = &buf;
  252.     len = (int)sprintf(bptr,str,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  253.     if (bufcnt+len >= BUFFERED) /* is there room */
  254.         {
  255.         tocopy = BUFFERED - bufcnt;
  256.         movmem(bptr, &out_buffer[bufcnt], tocopy);
  257.         bufcnt += tocopy;
  258.         len -= tocopy;
  259.         bptr += tocopy;
  260.         myfflush();
  261.         }
  262.     if (len)
  263.         {
  264.         /* just move it in */
  265.         movmem(bptr, &out_buffer[bufcnt], len);
  266.         bufcnt += len;
  267.         }
  268.    }
  269.  
  270. inchar()
  271.    {
  272.    struct IntuiMessage *Message, *GetMsg();
  273.    int c;
  274.    USHORT thismenu, menusel;
  275.    struct Gadget *gadget;
  276.    struct MenuItem *item, *ItemAddress();
  277.    c = 0;
  278.    while(!c)
  279.       {
  280.       while( (Message = GetMsg(HackWindow->UserPort)) == NULL)
  281.          Wait( 1 << HackWindow->UserPort->mp_SigBit );
  282.  
  283.       switch(Message->Class)
  284.     {
  285.     case MENUPICK:
  286.         menusel = thismenu = Message->Code;
  287.         while(thismenu != MENUNULL)
  288.            {
  289.            menusel = thismenu;
  290.            item = ItemAddress(&HackMenu, thismenu);
  291.            thismenu = item->NextSelect;
  292.            }
  293.         if (menusel != MENUNULL)
  294.            c = menukey[MENUNUM(menusel)][ITEMNUM(menusel)];
  295.         break;
  296.     case MOUSEBUTTONS:
  297.         mousex = ( (Message->MouseX) + BASEX ) / XSIZE;
  298.          mousey = ( (Message->MouseY) - BASEY ) / YSIZE;
  299.         if (mousex > 0 && mousey > 0 &&
  300.             mousex <= COLNO && mousey <= ROWNO )
  301.             {
  302.             if (Message->Code == SELECTDOWN)
  303.                 c = MDOWN;
  304.             else if (Message->Code == SELECTUP)
  305.                 c = MUP;
  306.             }
  307.         break;
  308.     case CLOSEWINDOW:
  309.         c = 'Q';
  310.         break;
  311.     case GADGETDOWN:
  312.         gadget = (struct Gadget *)Message->IAddress;
  313.         c = gadget->GadgetID;
  314.         break;
  315.     case RAWKEY:
  316.         c = cnvrtkey(Message->Code,Message->Qualifier);
  317.         break;
  318.     default:
  319.         c = 'Q';
  320.         break;
  321.     }
  322.       ReplyMsg(Message);
  323.       }
  324.    return(c);
  325.    }
  326.  
  327. #define NORMAL 0
  328. #define SHIFTED 1
  329. #define CONTROL 2
  330. #define ALTED 3
  331. short lookup[4][96] =
  332. {
  333. /* unshifted table */
  334.     '`',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  335.     '8',    '9',    '0',    '-',    '=',    '\\',    0,    '0',
  336.     'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',
  337.     'o',    'p',    '[',    ']',    0,    'b',    'j',    'n',
  338.     'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',
  339.     'l',    ';',    '\'',    0,    0,    'h',    '.',    'l',
  340.     0,    'z',    'x',    'c',    'v',    'b',    'n',    'm',
  341.     ',',    '.',    '/',    0,    '.',    'y',    'k',    'u',
  342.     ' ',    8,    'i',    '\n',    '\n',    022,    8,    0,
  343.     0,    0,    '-',    0,    'k',    'j',    'l',    'h',
  344.     0,    0,    0,    0,    0,    0,    0,    0,
  345.     0,    0,    0,    0,    0,    0,    0,    '?',
  346.  
  347. /* shifted table */
  348.     '~',    '!',    '@',    '#',    '$',    '%',    '^',    '&',
  349.     '*',    '(',    ')',    '_',    '+',    '|',    0,    '0',
  350.     'Q',    'W',    'E',    'R',    'T',    'Y',    'U',    'I',
  351.     'O',    'P',    '{',    '}',    0,    'B',    'J',    'N',
  352.     'A',    'S',    'D',    'F',    'G',    'H',    'J',    'K',
  353.     'L',    ':',    '"',    0,    0,    'H',    '.',    'L',
  354.     0,    'Z',    'X',    'C',    'V',    'B',    'N',    'M',
  355.     '<',    '>',    '?',    0,    '.',    'Y',    'K',    'U',
  356.     ' ',    'H',    'I',    '\N',    '\N',    022,    'H',    0,
  357.     0,    0,    '-',    0,    'K',    'J',    'L',    'H',
  358.     0,    0,    0,    0,    0,    0,    0,    0,
  359.     0,    0,    0,    0,    0,    0,    0,    '?',
  360.  
  361. /* controlled table */
  362.     0,    0,    0,    0,    0,    0,    0,    0,
  363.     0,    0,    0,    0,    0,    'Q',    0,    0,
  364.     0,    0,    0,    022,    024,    0,    0,    0,
  365.     0,    020,    0,    0,    0,    0,    0,    0,
  366.     0,    0,    0,    0,    0,    0,    0,    0,
  367.     0,    0,    0,    0,    0,    0,    0,    0,
  368.     0,    0,    0,    0,    0,    0,    0,    0,
  369.     0,    0,    0,    0,    0,    0,    0,    0,
  370.     0,    0,    0,    0,    0,    0,    0,    0,
  371.     0,    0,    0,    0,    0,    0,    0,    0,
  372.     0,    0,    0,    0,    0,    0,    0,    0,
  373.     0,    0,    0,    0,    0,    0,    0,    '?',
  374.  
  375. /* alted table */
  376.     '`',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
  377.     '8',    '9',    '0',    '-',    '=',    '\\',    0,    '0',
  378.     'q',    'w',    'e',    'r',    't',    'y',    'u',    'i',
  379.     'o',    'p',    '[',    ']',    0,    'b',    'j',    'n',
  380.     'a',    's',    'd',    'f',    'g',    'h',    'j',    'k',
  381.     'l',    ';',    '\'',    0,    0,    'h',    '.',    'l',
  382.     0,    'z',    'x',    'c',    'v',    'b',    'n',    'm',
  383.     ',',    '.',    '?',    0,    '.',    'y',    'k',    'u',
  384.     ' ',    'h',    'i',    '\n',    '\n',    022,    'h',    0,
  385.     0,    0,    '-',    0,    'k',    'j',    'l',    'h',
  386.     0,    0,    0,    0,    0,    0,    0,    0,
  387.     0,    0,    0,    0,    0,    0,    0,    '?'
  388. };
  389.  
  390. int cnvrtkey(code, qual )
  391. USHORT code, qual;
  392.    {
  393.    int table;
  394.  
  395.    if (code > 0x5f)
  396.       return(0);
  397.  
  398.    if (qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT))
  399.       table = SHIFTED;
  400.    else if (qual & (IEQUALIFIER_LALT | IEQUALIFIER_RALT))
  401.        table = ALTED;
  402.    else if (qual & (IEQUALIFIER_CONTROL))
  403.       table = CONTROL;
  404.    else
  405.       table = NORMAL;
  406.    return((int)lookup[table][code]);
  407.    }
  408.